package com.gxa.modules.service.impl;

import com.gxa.common.utils.Constants;
import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.R;
import com.gxa.modules.mapper.WaybillMapper;
import com.gxa.modules.pojo.Start_List;
import com.gxa.modules.pojo.Waybill;
import com.gxa.modules.service.WaybillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Auth WK
 * @DATE 2022/9/14 2:27
 * @DESCRIBE
 */
@Service
public class WaybillServiceImpl implements WaybillService {
    @Autowired
    private WaybillMapper waybillMapper;
    R r = new R();
    JsonResult jsonResult = new JsonResult();

    /**
     * 1.运单列表查询
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult findWaybillList(Integer currentPage, Integer pageSize) {
        //总条数
        int count = waybillMapper.findCountOfWaybill(Constants.STATUS_way4);
        Integer start = (currentPage - 1) * pageSize;//分页第一个参数
        Integer off = pageSize; //分页第二个参数
        List<Map> waybillList = waybillMapper.findWaybillList(start,off, Constants.STATUS_way4);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setCount(count);
        jsonResult.setData(waybillList);
        return jsonResult;
    }

    /**
     * 2.新增运单
     * @param waybill
     * @return
     */
    @Override
    public R addWaybill(Waybill waybill) {
        int result = waybillMapper.addWaybill(waybill);
        if (result>0){
            r.setCode(200);
            r.setMsg("添加成功");
        }
        return r;
    }

    /**
     * 3.运单配载
     * @param waybillId
     * @return
     */
    @Override
    public R stowageWaybill(Integer waybillId) {
        int result = waybillMapper.stowageWaybill(waybillId,Constants.STATUS_way2);
        if (result>0){
            r.setCode(200);
            r.setMsg("操作成功");
        }
        return r;
    }

    /**
     * 4.运单确认到达
     * @param waybillId
     * @return
     */
    @Override
    public R updateWaybill(Integer waybillId) {
        int result = waybillMapper.updateWaybill(waybillId, Constants.STATUS_way3);
        if (result>0){
            r.setCode(200);
            r.setMsg("操作成功");
        }
        return r;
    }

    /**
     * 5.查看运单详情
     * @param waybillId
     * @return
     */
    @Override
    public JsonResult findWaybill(Integer waybillId) {
        Map waybill = waybillMapper.findWaybill(waybillId);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setData(waybill);
        return jsonResult;
    }

    /**
     * 6.删除运单
     * @param waybillId
     * @return
     */
    @Override
    public R delectWaybill(Integer waybillId) {
        int result = waybillMapper.delectWaybill(waybillId, Constants.STATUS_way4);
        if (result>0){
            r.setCode(200);
            r.setMsg("删除成功");
        }
        return r;
    }

    /**
     * 7.条件搜索运单
     * @param waybillNumber
     * @param customerName
     * @param payTime
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult findWaybillByCondition(String waybillNumber, String customerName, String payTime, Integer currentPage, Integer pageSize) {
        //总条数
        int count = waybillMapper.findCountOfWaybillByCondition(Constants.STATUS_way4);
        Integer start = (currentPage - 1) * pageSize;//分页第一个参数
        Integer off = pageSize; //分页第二个参数
        List<Map> waybills = waybillMapper.findWaybillByCondition(waybillNumber,customerName,payTime,currentPage,pageSize, Constants.STATUS_way4);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setCount(count);
        jsonResult.setData(waybills);
        return jsonResult;
    }

    /**
     * 8.运单查询（待配载)
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult findWaybillByStowage1(Integer currentPage, Integer pageSize) {
        //总条数
        int count = waybillMapper.findCountOfWaybillByStowage1(Constants.STATUS_way1);
        Integer start = (currentPage - 1) * pageSize;//分页第一个参数
        Integer off = pageSize; //分页第二个参数
        List<Map> waybills = waybillMapper.findWaybillByStowage1(start,off, Constants.STATUS_way1);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setCount(count);
        jsonResult.setData(waybills);
        return jsonResult;

    }

    /**
     * 9.运单查询（已配载）
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult findWaybillByStowage2(Integer currentPage, Integer pageSize) {
        //总条数
        int count = waybillMapper.findCountOfWaybillByStowage2(Constants.STATUS_way2);
        Integer start = (currentPage - 1) * pageSize;//分页第一个参数
        Integer off = pageSize; //分页第二个参数
        List<Map> waybills = waybillMapper.findWaybillByStowage2(start,off, Constants.STATUS_way2);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setCount(count);
        jsonResult.setData(waybills);
        return jsonResult;
    }

    /**
     * 10.运单查询（已完成）
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult findWaybillByStowage3(Integer currentPage, Integer pageSize) {
        //总条数
        int count = waybillMapper.findCountOfWaybillByStowage3(Constants.STATUS_way3);
        Integer start = (currentPage - 1) * pageSize;//分页第一个参数
        Integer off = pageSize; //分页第二个参数
        List<Map> waybills = waybillMapper.findWaybillByStowage3(start,off, Constants.STATUS_way3);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setCount(count);
        jsonResult.setData(waybills);
        return jsonResult;
    }

    /**
     * 11.批量删除运单
     * @param waybillIds
     * @return
     */
    @Override
    public R delectWaybillList(Integer[] waybillIds) {
        if (waybillIds.length > 0) {
            for (Integer waybillId : waybillIds) {
                waybillMapper.delectWaybillList(waybillId, Constants.STATUS_way4);
            }
        }
        r.setCode(200);
        r.setMsg("批量删除成功");
        return r;
    }
}
